﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_quan_ly_home_slide : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAllRes();
            GetAllHomeSlide();
        }
    }
    protected void GetAllRes()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllResHaveNoSlide";
            DataTable db = Dal.GetDataTable();
            ddlRes.DataSource = db;
            ddlRes.DataTextField = db.Columns["TenNhaHang"].ToString();
            ddlRes.DataValueField = db.Columns["NhaHangID"].ToString();
            ddlRes.DataBind();
        }
        catch { }
    }
    protected void GetAllHomeSlide()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllHomeSlide";
            DataTable db = Dal.GetDataTable();
            grvHomeSlide.DataSource = db;
            grvHomeSlide.DataBind();
        }
        catch { }
        
    }
    protected void grvHomeSlide_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvHomeSlide.EditIndex = -1;
        GetAllHomeSlide();
    }
    protected void grvHomeSlide_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(grvHomeSlide.DataKeys[e.RowIndex].Value);
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteHomeSlide";
            Dal.ParameterCollection = new string[1] { "@SlideID"};
            Dal.ValueCollection = new string[1] {Convert.ToString(id)};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllHomeSlide();

        }
        catch { }
    }
    protected void grvHomeSlide_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvHomeSlide.EditIndex = e.NewEditIndex;
        GetAllHomeSlide();
    }
    protected void grvHomeSlide_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtTitle = (TextBox)grvHomeSlide.Rows[e.RowIndex].FindControl("txtTitle");
        TextBox txtDescription = (TextBox)grvHomeSlide.Rows[e.RowIndex].FindControl("txtDescription");
        TextBox txtStartDay = (TextBox)grvHomeSlide.Rows[e.RowIndex].FindControl("txtDayStart");
        AjaxControlToolkit.CalendarExtender cldStartDay = (AjaxControlToolkit.CalendarExtender)grvHomeSlide.Rows[e.RowIndex].FindControl("CalendarExtender1");
        TextBox txtEndDay = (TextBox)grvHomeSlide.Rows[e.RowIndex].FindControl("txtDayEnd");
        AjaxControlToolkit.CalendarExtender cldEndDay = (AjaxControlToolkit.CalendarExtender)grvHomeSlide.Rows[e.RowIndex].FindControl("CalendarExtender2");
        FileUpload fuBanner = (FileUpload)grvHomeSlide.Rows[e.RowIndex].FindControl("fuAvatar");
        CheckBox cbActive = (CheckBox)grvHomeSlide.Rows[e.RowIndex].FindControl("cbActive");
        CheckBox cbOnHome = (CheckBox)grvHomeSlide.Rows[e.RowIndex].FindControl("cbOnHome");
        int id = Convert.ToInt32(grvHomeSlide.DataKeys[e.RowIndex].Value);
        if (fuBanner.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(fuBanner.FileName);
                fuBanner.SaveAs(Server.MapPath("~/Images/HomeSlide/" + filename));
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_UpdateHomeSlideWithImage";
                Dal.ParameterCollection = new string[8] { "@TieuDe", "@MoTa", "@NgayBatDau", "@NgayKetThuc", "@DuongDan", "@IsActive", "@OnHome", "@SlideID" };
                Dal.ValueCollection = new string[8] { txtTitle.Text, txtDescription.Text, txtStartDay.Text, txtEndDay.Text, "Images/HomeSlide/" + filename, (cbActive.Checked).ToString(), cbOnHome.Checked.ToString(), Convert.ToString(id) };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
            }
            catch { }
        }
        else
        {
            try {
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_UpdateHomeSlideWithNoneImage";
                Dal.ParameterCollection = new string[7] { "@TieuDe", "@MoTa", "@NgayBatDau", "@NgayKetThuc", "@IsActive", "@OnHome", "@SlideID" };
                Dal.ValueCollection = new string[7] { txtTitle.Text, txtDescription.Text, txtStartDay.Text, txtEndDay.Text, (cbActive.Checked).ToString(), cbOnHome.Checked.ToString(), Convert.ToString(id) };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
                }
            catch { }
        }
        grvHomeSlide.EditIndex = -1;
        GetAllHomeSlide();
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        try {
            if (fuResBanner.HasFile)
            {
                string filename = Path.GetFileName(fuResBanner.FileName);
                fuResBanner.SaveAs(Server.MapPath("~/Images/HomeSlide/" + filename));
            }
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_AddHomeSlide";
            Dal.ParameterCollection = new string[8] { "@NhaHangID", "@TieuDe", "@MoTa", "@NgayBatDau", "@NgayKetThuc", "@DuongDan", "@IsActive", "@OnHome" };
            Dal.ValueCollection = new string[8] { ddlRes.SelectedValue,txtResTitle.Text,txtResDescription.Text,txtStartDay.Text,txtEndDay.Text,"Images/HomeSlide/"+fuResBanner.FileName,rblActive.SelectedValue,rblOnHome.SelectedValue};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            lblInfo.Text = "Thêm mới thành công";
        }
        catch { }
    }

    protected void grvHomeSlide_DataBound(object sender, EventArgs e)
    {

    }
}
